package com.house.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.User;
import com.house.common.core.page.TableDataInfo;
import com.house.system.service.IHouseService;
import com.house.system.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.house.system.domain.Report;
import com.house.system.service.IReportService;

/**
 * 用户报障对象Controller
 *
 * @author tc
 * @date 2022-02-25
 */
@RestController
@RequestMapping("/domain/report")
public class ReportController extends BaseController {
    @Autowired
    private IReportService reportService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IHouseService houseService;

    /**
     * 查询用户报障对象列表
     */
    @PreAuthorize("@ss.hasPermi('domain:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(Report report) {
        startPage();
        List<Report> list = reportService.selectReportList(report);
        return getDataTable(list);
    }

    /**
     * 获取用户报障对象详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:report:query')")
    @GetMapping(value = "/{reportId}/{houseId}/{userId}")
    public AjaxResult getDetail(@PathVariable("reportId") Long reportId, @PathVariable("houseId") Long houseId, @PathVariable("userId") Long userId) {
        AjaxResult ajax = AjaxResult.success();
        House house = houseService.selectHouseByHouseId(houseId);
        User user = userService.selectUserByUserId(userId);
        Report report = reportService.selectReportByReportId(reportId);
        ajax.put("house", house);
        ajax.put("user", user);
        ajax.put("report", report);
        return ajax;
    }

    /**
     * 获取用户报障对象详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:report:query')")
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Long reportId) {

        return AjaxResult.success(reportService.selectReportByReportId(reportId));
    }


    /**
     * 新增用户报障对象
     */
    @PreAuthorize("@ss.hasPermi('domain:report:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Report report) {
        report.setUserId(getUserId());
        return toAjax(reportService.insertReport(report));
    }

    /**
     * 修改用户报障对象
     */
    @PreAuthorize("@ss.hasPermi('domain:report:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Report report) {
        return toAjax(reportService.updateReport(report));
    }

    /**
     * 中介处理报障
     */
    @PreAuthorize("@ss.hasPermi('domain:report:handle')")
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Report report) {
        report.setIsHandle("1");
        return toAjax(reportService.updateReport(report));
    }

    /**
     * 删除用户报障对象
     */
    @PreAuthorize("@ss.hasPermi('domain:report:remove')")
    @DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds) {
        return toAjax(reportService.deleteReportByReportIds(reportIds));
    }
}
